package com.gr.smartedubackend.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.constant.CommonConstant;
import com.gr.smartedubackend.model.dto.student_group.StudentGroupQueryByTeacherDto;
import com.gr.smartedubackend.model.dto.student_group.StudentGroupQueryRequest;
import com.gr.smartedubackend.model.entity.StudentGroup;
import com.gr.smartedubackend.model.entity.Teacher;
import com.gr.smartedubackend.model.entity.TeachingSchedule;
import com.gr.smartedubackend.service.StudentGroupService;
import com.gr.smartedubackend.mapper.StudentGroupMapper;
import com.gr.smartedubackend.service.TeacherService;
import com.gr.smartedubackend.service.TeachingScheduleService;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 尘轩
 * @description 针对表【student_group(班级)】的数据库操作Service实现
 * @createDate 2025-07-01 17:03:05
 */
@Service
public class StudentGroupServiceImpl extends ServiceImpl<StudentGroupMapper, StudentGroup>
        implements StudentGroupService {

    @Resource
    private TeacherService teacherService;

    @Resource
    private TeachingScheduleService teachingScheduleService;

    @Override
    public QueryWrapper<StudentGroup> getQueryWrapper(StudentGroupQueryRequest studentGroupQueryRequest) {

        ThrowUtils.throwIf(studentGroupQueryRequest == null, ErrorCode.PARAMS_ERROR);
        String className = studentGroupQueryRequest.getClassName();
        String grade = studentGroupQueryRequest.getGrade();
        String teacherName = studentGroupQueryRequest.getTeacherName();
        Integer studentNum = studentGroupQueryRequest.getStudentNum();
        String sortField = studentGroupQueryRequest.getSortField();
        String sortOrder = studentGroupQueryRequest.getSortOrder();

        QueryWrapper<StudentGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(className), "className", className);
        queryWrapper.like(StrUtil.isNotBlank(grade), "grade", grade);
        queryWrapper.like(StrUtil.isNotBlank(teacherName), "teacherName", teacherName);
        queryWrapper.eq(ObjectUtil.isNotEmpty(studentNum), "studentNum", studentNum);
        if (StrUtil.isNotEmpty(sortOrder) && StrUtil.isNotEmpty(sortField)) {
            queryWrapper.orderBy(true, sortOrder.equals("ascend"), sortField);
        } else {
            queryWrapper.orderByDesc(CommonConstant.CREATE_TIME);
        }

        return queryWrapper;
    }

    @Override
    public Page<StudentGroup> listStudentGroupByPageByTeacher(StudentGroupQueryByTeacherDto studentGroupQueryByTeacherDto) {

        String teacherNumber = studentGroupQueryByTeacherDto.getTeacherNumber();
        int current = studentGroupQueryByTeacherDto.getCurrent();
        int pageSize = studentGroupQueryByTeacherDto.getPageSize();
        String sortField = studentGroupQueryByTeacherDto.getSortField();
        String sortOrder = studentGroupQueryByTeacherDto.getSortOrder();
        Page<StudentGroup> studentGroupPage = new Page<>(current, pageSize);

        // 查询教师id
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("teacherNumber", teacherNumber);
        Teacher teacher = teacherService.getOne(teacherQueryWrapper);
        ThrowUtils.throwIf(teacher == null, ErrorCode.NOT_FOUND_ERROR, "教师不存在");
        Long teacherId = teacher.getId();

        // 查询教师授课记录，再根据授课记录查询授课班级
        QueryWrapper<TeachingSchedule> teachingScheduleQueryWrapper = new QueryWrapper<>();
        teachingScheduleQueryWrapper.eq("teacherId", teacherId);
        List<TeachingSchedule> teachingScheduleList = teachingScheduleService.list(teachingScheduleQueryWrapper);
        if (teachingScheduleList.isEmpty()) {
            return studentGroupPage;
        }
        Set<Long> classIdList = teachingScheduleList.stream().map(TeachingSchedule::getClassId).collect(Collectors.toSet());
        QueryWrapper<StudentGroup> studentGroupQueryWrapper = new QueryWrapper<>();
        studentGroupQueryWrapper.in("id", classIdList);
        if (StrUtil.isNotEmpty(sortOrder) && StrUtil.isNotEmpty(sortField)) {
            studentGroupQueryWrapper.orderBy(true, sortOrder.equals("ascend"), sortField);
        } else {
            studentGroupQueryWrapper.orderByDesc(CommonConstant.CREATE_TIME);
        }

        return this.page(studentGroupPage, studentGroupQueryWrapper);
    }
}




